/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utils;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Administrateur
 */
public class TableModel extends AbstractTableModel {
    private Connexion _connexion;
    private  Object[][] data;
    private  Object[] colname;

    public TableModel(Connexion connect, String query,String[] col) throws SQLException
    {
        this.colname = col;
        this._connexion = connect;
        this._connexion.setQuery(query);
    }

    public int getColumnCount()
    {
        return this.colname.length;
    }

    public int getRowCount()
    {
        int i = 0;
        try {
            ResultSet rs = this._connexion.getResultSet();
            rs.last();
            i = rs.getRow();
        }catch(SQLException e){
            System.out.println(e);
        }
        return i;
    }

    @Override
    public String getColumnName(int c){
        return (String) this.colname[c];
    }

    public Object getValueAt(int row,int column)
    {
        Object o = "";
        try {
            ResultSet rs = this._connexion.getResultSet();
            rs.absolute(row + 1);
            o = rs.getObject(column + 1);
        }catch(SQLException e){
            System.out.println(e);
        }
        return o;
    }
}
